/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.allerj.model;

import it.allerj.common.persistence.PersistenceObject;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Alessandro Veracchi
 */
public class PersistedMatrix extends PersistenceObject {

    private List<RowMatrix> rowList;
    //private int length;
    private String name;

    //ONLY FOR HIBERNATE
    public PersistedMatrix() {

    }

    public PersistedMatrix(int length, int height) {
        //this.length = length;
        this.rowList = new ArrayList<RowMatrix>(height);
        if(rowList.size()==0){
            for (int i = 0; i < height; i++) {
                this.rowList.add(new RowMatrix(length));
                for (int j = 0; j < length; j++) {
                    this.rowList.get(i).setCell(null, j);
                }
            } 
        } else {
            for (int i = 0; i < height; i++) {
                this.rowList.set(i,new RowMatrix(length));
                for (int j = 0; j < length; j++) {
                    this.rowList.get(i).setCell(null, j);
                }
            }
        }

    }

    public RowMatrix getRow(int position) {
        RowMatrix row = null;
        //if ((position >= 0) && (position < rowList.get(position))) {
            row = rowList.get(position);
        //}
        return row;
    }
    public void setRow(RowMatrix row, int position) {
        //if (row!=null && position >= 0 && position < length) {
            rowList.set(position,row);
        //}
    }

    public int getLength() {
        if(rowList!=null && rowList.get(0)!=null && rowList.get(0).getCellList()!=null )
        return rowList.get(0).getCellList().size();
        else return 0;
    }

    public int getHeight() {
        if(rowList!=null)
        return rowList.size();
        return 0;
    }

    public CellMatrix getElement(int row, int col) {
        int length = 0;
        if(rowList!=null && rowList.get(row)!=null && rowList.get(row).getCellList()!=null){
            length =  rowList.get(row).getCellList().size();
            if (row < 0 || row > length) {
                return null;
            } else if (col < 0 || col > rowList.size()) {
                return null;
            } else {
                return getRow(row).getCell(col);
            }
        }
        return null;
    }

    public void setElement(CellMatrix cell, int row, int col) {
        int length = 0;
        if(rowList!=null && rowList.get(row)!=null && rowList.get(row).getCellList()!=null)
            length =  rowList.get(row).getCellList().size();
        if (row < 0 || row > length) {
            return;
        } else if (rowList == null || col < 0 || col > rowList.size()) {
            return;
        } else {
            this.rowList.get(col).setCell(cell, row);
        }
    }

    private List<RowMatrix> getRowList() {
        return rowList;
    }

    private void setRowList(List<RowMatrix> rowMatrix) {
        this.rowList = rowMatrix;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
